package org.acsis.covoiturage.frontend.actions;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.acsis.covoiturage.frontend.application.SVC;
import org.acsis.covoiturage.frontend.forms.RechercheForm;
import org.acsis.covoiturage.services.ITrajetServices;
import org.acsis.covoiturage.shared.AlreadySubscribedException;
import org.acsis.covoiturage.shared.IClient;
import org.acsis.covoiturage.shared.ITrajet;
import org.acsis.covoiturage.shared.IUtilisateur;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.actions.DispatchAction;

public class RechercheAction extends DispatchAction {

	/* (non-Javadoc)
	 * @see org.apache.struts.action.Action#execute(org.apache.struts.action.ActionMapping, org.apache.struts.action.ActionForm, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	public ActionForward recherche(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		RechercheForm rechercheForm = (RechercheForm)form;
		ITrajetServices services = SVC.create(ITrajetServices.class);
		String villeDepart=rechercheForm.getVilleDepart();
//		String villeArrive=rechercheForm.getVilleArrive();
		String dateDepart=rechercheForm.getDateDepart();
		Date dDepart=  (new SimpleDateFormat("dd/MM/yyyy")).parse(dateDepart);
		List<ITrajet> iTrajets = services.rechercher(dDepart, villeDepart);
		if (iTrajets!=null) {
			rechercheForm.setTrajets(iTrajets);
			return mapping.findForward("successrecherche");
		} else {
			ActionErrors errors = new ActionErrors();
			errors.add("rechercheEchec",new ActionMessage("erreur.rechercheEchec"));
			saveErrors(request, errors);
			return mapping.findForward("successrecherche");
		}
		
	}
	
	public ActionForward abbonner(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		// TODO Auto-generated method stub
		try {
			int idTrajet = Integer.parseInt(request.getParameter("choice"));
			HttpSession session = request.getSession();
			IUtilisateur utilisateur = (IUtilisateur) session.getAttribute("user");
			IClient client = utilisateur.getPersonne();
			ITrajetServices services = SVC.create(ITrajetServices.class);
			
			ITrajet track = null;
			
			try{
				track= services.souscrire(idTrajet, client.getIdClient().intValue());
			}
			catch (AlreadySubscribedException e) {
				e.printStackTrace();
			}
			
			if (track != null) {
				RechercheForm rechercheForm= (RechercheForm)form;
				rechercheForm.setTrajets(null);
				return mapping.findForward("successabbonner");
			} else {
				ActionErrors errors = new ActionErrors();
				errors.add("abbonnerDeja",new ActionMessage("erreur.abbonnerDeja"));
				saveErrors(request, errors);
				return mapping.findForward("failedabbonner");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			ActionErrors errors = new ActionErrors();
			errors.add("rechercheChoix",new ActionMessage("erreur.rechercheChoix"));
			saveErrors(request, errors);
			return mapping.findForward("successabbonner");
		}
		
		
		
	}

}
